<?php
/*
Plugin Name: PayPal Pro
Plugin URI: http://www.blueskyis.com/
Version: v0.09
Author: <a href="http://technokrafts.net">Technokrafts</a>
Description: A plugin for <a href="http://code.google.com/p/paypal-pro-wordpress-gateway/">PayPal Pro gateway for ZetaPrints</a>. 
*/
if (!class_exists("PayPalPro")) {
	class PayPalPro {
		var $adminOptionsName = "PayPalProAdminOptions";
	
		function PayPalPro() { //constructor
		}
		
		function init() {
			$this->getAdminOptions();
		}
		
		//Returns an array of admin options
		function getAdminOptions() {
			$paypalproAdminOptions = array('api_username'=>'','api_password'=>'',
				'api_signature'=>'', 'api_endpoint'=>'', 'paypal_url'=>'','charge_amount'=>'');
			$devOptions = get_option($this->adminOptionsName);
			if (!empty($devOptions)) {
				foreach ($devOptions as $key => $option) {
					$paypalproAdminOptions[$key] = $option;
				}
			}
			
			update_option($this->adminOptionsName, $paypalproAdminOptions);
			return $paypalproAdminOptions;
		}
		
		
		// Prints out the admin page
		function printAdminPage() {
			$devOptions = $this->getAdminOptions();

			if (isset($_POST['update_PaypalProSettings'])) {
				if (isset($_POST['api_username'])) {
					$devOptions['api_username'] = $_POST['api_username'];
				}
				if (isset($_POST['api_password'])) {
					 $devOptions['api_password'] = $_POST['api_password'];
				}
				if (isset($_POST['api_signature'])) {
					$devOptions['api_signature'] = $_POST['api_signature'];
				}
				if (isset($_POST['api_endpoint'])) {
					$devOptions['api_endpoint'] = $_POST['api_endpoint'];
				}
				if (isset($_POST['paypal_url'])) {
					$devOptions['paypal_url'] = $_POST['paypal_url'];
				}
				if (isset($_POST['api_key'])) {
					$devOptions['api_key'] = $_POST['api_key'];
				}
				if (isset($_POST['admin_email'])) {
					$devOptions['admin_email'] = $_POST['admin_email'];
				}
				if (isset($_POST['charge_amount'])) {
					$devOptions['charge_amount'] = $_POST['charge_amount'];
				}
				if (isset($_POST['pay_form_title'])) {
					$devOptions['pay_form_title'] = $_POST['pay_form_title'];
				}				
				if (isset($_POST['email_subject'])) {
					$devOptions['email_subject'] = $_POST['email_subject'];
				}
				if (isset($_POST['email_from'])) {
					$devOptions['email_from'] = $_POST['email_from'];
				}
				if (isset($_POST['notification_email_addr'])) {
					$devOptions['notification_email_addr'] = $_POST['notification_email_addr'];
				}
				update_option($this->adminOptionsName, $devOptions);
?>
<div class="updated"><p><strong><?php _e("Settings Updated.", "PayPalPro");?></strong></p></div>
<?php
			} ?>
			
<div class=wrap>
<form method="post" action="<?php echo $_SERVER["REQUEST_URI"]; ?>">
<h2>PayPal Pro Plugin</h2>
<h3>API Username</h3>
<input type='text' name='api_username' value='<?php _e($devOptions['api_username'],'PayPalPro') ?>' size='50'>

<h3>API Password</h3>
<input type='text' name='api_password' value='<?php _e($devOptions['api_password'],'PayPalPro') ?>' size='20'>

<h3>API Signature</h3>
<input type='text' name='api_signature' value='<?php _e($devOptions['api_signature'],'PayPalPro') ?>' size='70'>

<h3>API Endpoint</h3>
<input type='text' name='api_endpoint' value='<?php _e($devOptions['api_endpoint'],'PayPalPro') ?>' size='50'>

<h3>PayPal URL</h3>
<input type='text' name='paypal_url' value='<?php _e($devOptions['paypal_url'],'PayPalPro') ?>' size='70'>

<h3>ZetaPrints API Key</h3>
<input type='text' name='api_key' value='<?php _e($devOptions['api_key'],'PayPalPro') ?>' size='70'>

<h3>Send failure reports to</h3>
<input type='text' name='admin_email' value='<?php _e($devOptions['admin_email'],'PayPalPro') ?>' size='70'>

<!--h3>Charge Amount</h3>
$<input type='text' name='charge_amount' value='<?php //_e($devOptions['charge_amount'],'PayPalPro') ?>' size='6'-->

<h3>Failure Notification Email Subject</h3>
<input type='text' name='email_subject' value='<?php _e($devOptions['email_subject'],'PayPalPro') ?>' size='50'>

<h3>Payment Form Title</h3>
<input type='text' name='pay_form_title' value='<?php _e($devOptions['pay_form_title'],'PayPalPro') ?>' size='50'>


<!--h3>Payment Notification Email FROM Address</h3>
<input type='text' name='email_from' value='<?php //_e($devOptions['email_from'],'PayPalPro') ?>' size='30'>

<h3>Payment Notification Email Address</h3>
<input type='text' name='notification_email_addr' value='<?php //_e($devOptions['notification_email_addr'],'PayPalPro') ?>' size='30'-->

<div class="submit">
<input type="submit" name="update_PaypalProSettings"
value="<?php _e('Update Settings', 'PayPalPro') ?>" /></div>

</form>
</div>
<?php
		}//End function printAdminPage()	
	}
} // End Class PayPalPro ////////////////


if (class_exists("PayPalPro")) {
	$dl_paypalpro = new PayPalPro();
}


//Actions and Filters
if (isset($dl_paypalpro)) {
	include_once(WP_PLUGIN_DIR . '/paypal-pro/php/paypal-pro-functions.php');

	//Initialize the admin panel
	if (!function_exists("PayPalPro_ap")) {
		function PayPalPro_ap() {
			global $dl_paypalpro;
			if (!isset($dl_paypalpro)) {
				return;
			}
			if (function_exists('add_options_page')) {
				add_options_page('PayPal Pro Plugin', 'PayPalPro', 9, basename(__FILE__), array(&$dl_paypalpro, 'printAdminPage'));
			}
		}
	}
	
	if (!function_exists("PP_Amount")) {
		function PP_amount() {
			global $dl_paypalpro;
			$pp_opts = $dl_paypalpro->getAdminOptions();			
			$pp_amount = ($pp_opts['charge_amount'] > 0)?$pp_opts['charge_amount']:99;
			return $pp_amount;
		}
	}
	
	if(!isset($_POST['submitpayment'])){
		$address1 	= trim(stripslashes($_POST['address1']));
		$city 		= trim(stripslashes($_POST['city']));
		$state 		= trim(stripslashes($_POST['state']));
		$zip 		= trim(stripslashes($_POST['zip']));
		$country 	= trim(stripslashes($_POST['country']));
		$eaddr 		= trim(stripslashes($_POST['eaddr']));
		$item_name	= trim(stripslashes($_POST['item_name']));
		$amount		= trim(stripslashes($_POST['amount']));
		$OrderID	= trim(stripslashes($_POST['return']));
		$cancel_return	= trim(stripslashes($_POST['cancel_return']));
		$notify_url	= trim(stripslashes($_POST['notify_url']));
	}
	
	if (!function_exists("PP_Form")) {
		function PP_Form() {
			global $dl_paypalpro, $pp_errors, $state_select, $province_select, $country_select, $credit_cards, $paypalproAdminOptions,
				$fname, $lname, $address1, $city, $state, $zip, $country, $eaddr, $cc_type, $cc_num, $cc_exp, $cc_cvc, $pay_success,$amount,$OrderID,$notify_url,$item_name,$cancel_return;
			//$spacer = "<tr><td colspan='2'>&nbsp;</td></tr>";
			
			$pp_opts = $dl_paypalpro->getAdminOptions();			
			//$pp_amount = PP_amount();
?>
<form method='POST' action='<?php echo $_SERVER['REQUEST_URI'];?>'>
<table id="payPalPro">
	<tr>
	  <td colspan='2'><table id='payPalProInner'>
          <tr>
            <td colspan="2"  class="productbox">Your Selected Items: </td>
          </tr>
          <tr>
            <td><strong>Item Name </strong></td>
            <td><strong>Price</strong></td>
          </tr>
          <tr>
            <td><?php echo $item_name;?></td>
            <td><?php echo currency($amount);?></td>
          </tr>
          <tr>
            <td><div align="right"><strong>Total</strong></div></td>
            <td><?php echo currency($amount);?></td>
          </tr>
        </table></td>
    </tr>
	<tr>
		<td colspan='2' class="productbox"><?php echo $pp_opts['pay_form_title']; ?></td>
	</tr>
<?php


if (!$pay_success) {
	if (count($pp_errors) > 0) {
		echo "<tr><td valign='top' colspan='2' class='error'><b>Please correct the following:</b><br>";
		echo implode("<br>",$pp_errors);
		echo "</td></tr>";
	}
?>
	<tr>
		<th>First Name:</td>
		<td ><input type='text' name='fname' class="textfield" value='<?php echo htmlSafe($fname); ?>' size='30'></td>
	</tr>

	<tr>
		<th>Last Name:</td>
		<td><input type='text' name='lname' class="textfield" value='<?php echo htmlSafe($lname); ?>' size='30'></td>
	</tr>

	<tr>
		<th>Street Address:</td>
		<td><input type='text' name='address1' class="textfield" value='<?php echo htmlSafe($address1); ?>' size='30'></td>
	</tr>

	<tr>
		<th>City:</td>
		<td><input type='text' name='city' class="textfield" value='<?php echo htmlSafe($city); ?>' size='30'></td>
	</tr>

	<tr>
		<th>State/Province:</td>
		<td>
<?php
echo formSelect('state', $state, array_merge($state_select, $province_select), '-- Select --');
?>		</td>
	</tr>
<?php echo $spacer; ?>
	<tr>
		<th>Zip/Postal Code:</td>
		<td ><input type='text' name='zip' class="textfield" value='<?php echo htmlSafe($zip); ?>' size='10'></td>
	</tr>
<?php echo $spacer; ?>
	<tr>
		<th>Country:</td>
		<td>
<?php
echo formSelect('country', $country, $country_select);
?>		</td>
	</tr>
<?php echo $spacer; ?>
	<tr>
		<th>Email Address:</td>
		<td ><input type='text' name='eaddr' class="textfield" value='<?php echo htmlSafe($eaddr); ?>' size='30'></td>
	</tr>
	
<?php echo $spacer; ?>
<?php echo $spacer; ?>
<tr>
	<td colspan="2" class="productbox">Credit Card Information: </td>
  </tr>
<?php echo $spacer; ?>
	<tr>
		<th>Credit Card Type:</td>
		<td >
<?php
// credit card type
echo formSelect('cc_type', $cc_type, $credit_cards);
?>		</td>
	</tr>
<?php echo $spacer; ?>
	<tr>
		<th>Credit Card Number:</td>
		<td><input type='text' name='cc_num' class="textfield" value='<?php echo htmlSafe($cc_num); ?>' size='16'></td>
	</tr>
<?php echo $spacer; ?>
	<tr>
		<th>Credit Card Expiration:</td>
		<td>
<?php
echo expDateDropdown('cc_exp',$cc_exp);
?>		</td>
	</tr>
<?php echo $spacer; ?>
	<tr>
		<th>Credit Card Security Code:</td>
		<td ><input type='text' name='cc_cvc' class="textfield" value='<?php echo htmlSafe($cc_cvc); ?>' maxlength="3"></td>
	</tr>
<?php echo $spacer; ?>
	<tr>
		<th>Payment Amount:</td>
		<td ><?php echo currency($amount); ?></td>
	</tr>
	<input type="hidden" name="notify_url" value="<?php echo $notify_url; ?>">
	<input type="hidden" name="return" value="<?php echo $OrderID; ?>">
	<input type="hidden" name="cancel_return" value="<?php echo $cancel_return; ?>">
	<input type="hidden" name="item_name" value="<?php echo $item_name; ?>">

<?php echo $spacer; ?>	
	<tr>
		<td colspan="2">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<input type='submit' id="ccSecCode" name='submitpayment' value='Send Payment'>&nbsp;<input type='button' id="ccSecCode" name='cancelpayment' value='Cancel' onclick="location.href='<?PHP echo $cancel_return?>'"></td></tr>
<?php
} else { // PAY SUCCESS
header("location:".$OrderID);
exit;
?>
	<tr>
		<td  colspan='2'><b>Thank you for your payment! We have been notified of payment. You should receive an email receipt shortly.</b></td>
	</tr>

<?php
} // if not failed payment
?>
</table>

<input type="hidden" name="amount" value="<?php echo $amount; ?>">
</form>
<?php	
		} // END FUNCTION PP_Form()
		
		
		
		///////////////////////////////////////
		// Processes submitted payment form
		if (!function_exists("PP_ProcessPayment")) {
			function PP_ProcessPayment() {
				global $dl_paypalpro, $pp_errors, $pay_success, $credit_cards;
				global $fname, $lname, $address1, $city, $state, $zip, $country, $eaddr, $cc_type, $cc_num, $cc_exp, $cc_cvc;
				global $API_Endpoint,$version,$API_UserName,$API_Password,$API_Signature,$nvp_Header,$amount,$OrderID,$notify_url,$item_name,$API_KEY,$admin_email;
				
				$pay_success = FALSE;
				$pp_errors = array();
				// Handle payment submit, if any
				if ($_SERVER['REQUEST_METHOD'] == "POST" && isset($_POST['submitpayment'])) {
					// Validate data
					$fname 		= trim(stripslashes($_POST['fname']));
					$lname 		= trim(stripslashes($_POST['lname']));
					$address1 	= trim(stripslashes($_POST['address1']));
					$city 		= trim(stripslashes($_POST['city']));
					$state 		= trim(stripslashes($_POST['state']));
					$zip 		= trim(stripslashes($_POST['zip']));
					$country 	= trim(stripslashes($_POST['country']));
					$eaddr 		= trim(stripslashes($_POST['eaddr']));
					$item_name	= trim(stripslashes($_POST['item_name']));
					$OrderID	= trim(stripslashes($_POST['return']));
					$cancel_return	= trim(stripslashes($_POST['cancel_return']));
					$notify_url = trim(stripslashes($_POST['notify_url']));
					
					if ($fname == '' || $lname == '' || $address1 == '' || $city == '' || $zip == '' ||
						($country == 'US' && $state == '') || ($country == 'CA' && $state == '')) {
						
						$pp_errors[] = "All billing information is required.";
					}
					
					if (!validEmail($eaddr)) {
						$pp_errors[] = "A valid email address is required.";
					}
					
					$cc_type = $_POST['cc_type'];
					$cc_num = allNumbers($_POST['cc_num']);
					$cc_exp = $_POST['cc_exp'];
					$cc_cvc = allNumbers($_POST['cc_cvc']);
					
					if (strlen($cc_num) < 10) {
						$pp_errors['cc_num'] = "Invalid credit card number.";
					}
					
					if (strlen($cc_cvc) < 3) {
						$pp_errors['cc_cvc'] = "Invalid security code.";
					}
					
					$amount	=	$_POST['amount'];
					//$amount = PP_amount();
					// Attempt to process, if no errors
					if (count($pp_errors) == 0) {
						// CALLER SERVICE
						define('USE_PROXY',FALSE);
						define('VERSION', '3.0'); // REQUIRED
						
						$pp_opts 		= $dl_paypalpro->getAdminOptions();
						$API_UserName 	= $pp_opts['api_username'];
						$API_Password 	= $pp_opts['api_password'];
						$API_Signature 	= $pp_opts['api_signature'];
						$API_Endpoint 	= $pp_opts['api_endpoint'];
						$API_KEY 		= $pp_opts['api_key'];
						$admin_email	= $pp_opts['admin_email'];	
						$version		= VERSION;
						
						/**
						  * hash_call: Function to perform the API call to PayPal using API signature
						  * @methodName is name of API  method.
						  * @nvpStr is nvp string.
						  * returns an associtive array containing the response from the server.
						*/
				
						function hash_call($methodName,$nvpStr) {
							//declaring of global variables
							global $API_Endpoint,$version,$API_UserName,$API_Password,$API_Signature,$nvp_Header,$pp_errors,$API_KEY,$admin_email;

							//setting the curl parameters.
							$ch = curl_init();
							curl_setopt($ch, CURLOPT_URL,$API_Endpoint);
							curl_setopt($ch, CURLOPT_VERBOSE, 1);
						
							//turning off the server and peer verification(TrustManager Concept).
							curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
							curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
						
							curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
							curl_setopt($ch, CURLOPT_POST, 1);
							//if USE_PROXY constant set to TRUE in Constants.php, then only proxy will be enabled.
						   //Set proxy name to PROXY_HOST and port number to PROXY_PORT in constants.php 
							if(USE_PROXY)
							curl_setopt ($ch, CURLOPT_PROXY, PROXY_HOST.":".PROXY_PORT); 
						
							//NVPRequest for submitting to server
							$nvpreq="METHOD=".urlencode($methodName)."&VERSION=".urlencode($version)."&PWD=".urlencode($API_Password)."&USER=".urlencode($API_UserName)."&SIGNATURE=".urlencode($API_Signature).$nvpStr;
						
							//setting the nvpreq as POST FIELD to curl
							curl_setopt($ch,CURLOPT_POSTFIELDS,$nvpreq);
						
							//getting response from server
							$response = curl_exec($ch);
						
							//convrting NVPResponse to an Associative Array
							$nvpResArray=deformatNVP($response);
							$nvpReqArray=deformatNVP($nvpreq);
							$_SESSION['nvpReqArray']=$nvpReqArray;
						
							if (curl_errno($ch)) {
								// moving to display page to display curl errors
								$_SESSION['curl_error_no']=curl_errno($ch) ;
								$_SESSION['curl_error_msg']=curl_error($ch);
								$pp_errors[] = curl_error($ch);
							} else {
								//closing the curl
								curl_close($ch);
							}
						
							return $nvpResArray;
						}
				
						/** This function will take NVPString and convert it to an Associative Array and it will decode the response.
						  * It is usefull to search for a particular key and displaying arrays.
						  * @nvpstr is NVPString.
						  * @nvpArray is Associative Array.
						  */
						
						function deformatNVP($nvpstr) {
							$intial=0;
							$nvpArray = array();
						
							while(strlen($nvpstr)){
								//postion of Key
								$keypos= strpos($nvpstr,'=');
								//position of value
								$valuepos = strpos($nvpstr,'&') ? strpos($nvpstr,'&'): strlen($nvpstr);
						
								/*getting the Key and Value values and storing in a Associative Array*/
								$keyval=substr($nvpstr,$intial,$keypos);
								$valval=substr($nvpstr,$keypos+1,$valuepos-$keypos-1);
								//decoding the respose
								$nvpArray[urldecode($keyval)] =urldecode( $valval);
								$nvpstr=substr($nvpstr,$valuepos+1,strlen($nvpstr));
							}
							return $nvpArray;
						}
					
						// Attempt transaction...
						// Get required parameters from the web form for the request
						$paymentType =urlencode('Sale');
						
						// cc_type. one of Visa MasterCard Discover Amex
						if ($cc_type == 4) {
							$creditCardType = urlencode("Amex");
						} else {
							$creditCardType = urlencode($credit_cards[$cc_type]);
						}
						
						$creditCardNumber 	= 	urlencode($cc_num);
						$cvv2Number 		= 	urlencode($cc_cvc);
						$firstName 			=	urlencode($fname);
						$lastName 			=	urlencode($lname);
						$Baddress 			= 	urlencode($address1);
						$Bcity 				= 	urlencode($city);
						$Bstate 			=	urlencode($state);
						$Bzip 				= 	urlencode($zip);
						//$item_name 			= 	urlencode($item_name);
						
						$Bamount = urlencode(number_format($amount,2,'.',''));
						$currencyCode="USD";
				
						/* Construct the request string that will be sent to PayPal.
				
						   The variable $nvpstr contains all the variables and is a
				
						   name value pair string with & as a delimiter */
				
						$nvpstr="&PAYMENTACTION=$paymentType&AMT=$Bamount&CREDITCARDTYPE=$creditCardType";
						$nvpstr .= "&ACCT=$creditCardNumber&EXPDATE=". $cc_exp ."&CVV2=$cvv2Number";
						$nvpstr .= "&FIRSTNAME=$firstName&LASTNAME=$lastName&STREET=$Baddress&CITY=$Bcity&STATE=$Bstate";
						$nvpstr .= "&ZIP=$Bzip&COUNTRYCODE=$country&CURRENCYCODE=$currencyCode";
						
						// Add IP ADDRESS
						$nvpstr .= "&IPADDRESS=".urlencode($_SERVER['REMOTE_ADDR']);
						
						//die($nvpstr);
						// Make the API call to PayPal, using API signature.
						// The API response is stored in an associative array called $resArray
				
						$resArray = hash_call("doDirectPayment",$nvpstr);
				
				
						$ack = strtoupper($resArray["ACK"]);
				
						if(trim($ack)=="SUCCESS") {
						
							$_SESSION['TxnID'] = $resArray["TRANSACTIONID"];
							$_SESSION['amount'] = '';
							$transactionID=urlencode($_SESSION['TxnID']);
							$nvpStr_get="&TRANSACTIONID=$transactionID";
							$responseArray=hash_call("gettransactionDetails",$nvpStr_get);
							
$str	=	"mc_gross=&protection_eligibility=&address_status=".urlencode($responseArray['ADDRESSSTATUS'])."&payer_id=".urlencode($responseArray['PAYERID'])."&tax=".urlencode($responseArray['TAXAMT'])."&address_street=".urlencode($address1)."&payment_date=".urlencode($responseArray['ORDERTIME'])."&charset=&address_zip=".urlencode($zip)."&first_name=".urlencode($responseArray['FIRSTNAME'])."&mc_fee=&address_country_code=".urlencode($responseArray['COUNTRYCODE'])."&address_name=&notify_version=".urlencode($responseArray['VERSION'])."&custom=&payer_status=".urlencode($responseArray['PAYERSTATUS'])."&business=&address_country=&address_city=&quantity=".urlencode($responseArray['L_QTY0'])."&verify_sign=&payer_email=".urlencode($eaddr)."&txn_id=".urlencode($responseArray['TRANSACTIONID'])."&payment_type=".urlencode($responseArray['PAYMENTTYPE'])."&last_name=".urlencode($responseArray['LASTNAME'])."&address_state=&receiver_email=".urlencode($responseArray['RECEIVEREMAIL'])."&payment_fee=".urlencode($responseArray['FEEAMT'])."&receiver_id=".urlencode($responseArray['RECEIVERID'])."&txn_type=".urlencode($responseArray['TRANSACTIONTYPE'])."&item_name=".urlencode($item_name)."&mc_currency=".urlencode($responseArray['CURRENCYCODE'])."&item_number=&residence_country=".urlencode($responseArray['COUNTRYCODE'])."&receipt_id=".urlencode($responseArray['RECEIPTID'])."&handling_amount=&transaction_subject=&payment_gross=".urlencode($responseArray['AMT'])."&shipping=&payment_status=".urlencode($responseArray['PAYMENTSTATUS'])."&sales_tax=".urlencode($responseArray['SALESTAX'])."&address_owner=".urlencode($responseArray['ADDRESSOWNER'])."&correlation_id=".urlencode($responseArray['CORRELATIONID'])."&ack=".urlencode($responseArray['ACK'])."&build=".urlencode($responseArray['BUILD'])."&pending_reason=".urlencode($responseArray['PENDINGREASON'])."&reason_code=".urlencode($responseArray['REASONCODE'])."&ApiKey=".$API_KEY."";


							//$notify_url	=	$notify_url."&ApiKey=".$API_KEY;
							$ch_one = curl_init(); 
							curl_setopt($ch_one, CURLOPT_URL,$notify_url);
							curl_setopt($ch_one, CURLOPT_POST, 1);
							curl_setopt($ch_one, CURLOPT_POSTFIELDS, $str);
							$return_val	=	curl_exec ($ch_one);
							curl_close ($ch_one);
							// Send thank you email address/receipt
							$p_date = date("M d, Y");
							
							/*echo "<PRE>";
								echo "send info to ".$notify_url." = ".$str."<BR>";
								echo "return val ";
								print_r($return_val);
							echo "</PRE>";
							exit;*/
							$subject = $pp_opts['email_subject'];
							$message = "Thank you for your payment.
							The details of your payment are as follows:
							Payment from $fname $lname
							Payment Date: $p_date
							Payment Amount: \$$Bamount
							Thank You!
							";
							$headers = "From: ".$pp_opts['admin_email']."\r\n";
							$headers .= "Return-Path: ".$pp_opts['admin_email']."\r\n";
							//$headers .= "Content-Type: text/html;"; // Mime type
							if($return_val!='<ok/>'){	//	If notify_url failed.
								$subject = $pp_opts['email_subject'];
								$message = "The details of payment are as follows:
								Payment from: $fname $lname
								Payment Date: $p_date
								Payment Amount: \$$Bamount
								Transaction ID: $transactionID
								There is problem to notify url.";
								mail($pp_opts['admin_email'], $subject, $message, $headers);
							}else{
								/*$headers = "From: ".$pp_opts['email_from']."\r\n";
								$headers .= "Return-Path: ".$pp_opts['email_from']."\r\n";
								mail($eaddr, $subject, $message, $headers);
								if (validEmail($pp_opts['notification_email_addr'])) {
									mail($pp_opts['notification_email_addr'], $subject, $message, $headers);
								}*/
							}
							$pay_success = TRUE;
						} else {
 						//print_r($resArray);
						//exit;
// 						die($nvpstr);
							$PayStatus = 0;
							$err_msg = $resArray["L_LONGMESSAGE0"]."<BR>".$resArray["L_LONGMESSAGE1"];
							// Remove 'and type' from message
							$err_msg = str_replace(' and type','', $err_msg);
							$pp_errors[] = $err_msg;
						}
					}
				}
			}
		} // FUNCTION processPayment
		
	
		function DoPPActivity() {
			PP_ProcessPayment();
			PP_Form();
		}
	}



	//Actions
//	add_action('wp_head', array(&$dl_paypalpro, 'addHeaderCode'), 1);
	add_action('activate_paypal-pro/paypal-pro.php', array(&$dl_paypalpro, 'init'));	
	add_action('admin_menu', 'PayPalPro_ap');
	add_action('ppp_form','DoPPActivity');
	
	//Filters
}
$stylesheet =	get_theme_root_uri();
$style_url	=	ereg_replace("themes","plugins/paypal-pro/paypal.css",$stylesheet);
echo '<link rel="stylesheet" href="'.$style_url.'" type="text/css" media="screen" />';
?>